#include "adc.h"

void adc_init()
{
	SAR_CTRL = 0x1000;      // Set to Channel 3 
	SAR_CLKCTRL = 0x0064;   // Clock division of 0x7F
	SAR_PINCTRL = 0x3600;   // Power On
	SAR_GPOCTRL = 0x0000;   //  No channels configured as out
	SAR_DATA = 0x1000;      // Data channel set to 3
	wait_usec(100);
	return;
}

Int16 adc_read()
{
	Int16 value;
	SAR_CTRL = 0x9000;
	while((SAR_DATA & 0x8000) == 0); 
	while((SAR_DATA & 0x8000) != 0 || (SAR_CTRL & 0x8000 != 0) ); 
	value = (SAR_DATA & 0x3FF );
	return value;
	
}
